import { Channels, Channel } from "@/types/channel";
import { defineStore } from "pinia";
import { reqGetChannels, reqGetAllChannels } from '@/api/channels'
import useUserStore from "./user";
const useChannelStore = defineStore('channel', {
    state() {
        return {
            channelList: [] as Channels,
            allChannelsList: [] as Channels
        }
    },
    actions: {
        // 拿用户自己的channels
        async getChannels() {
            const { data } = await reqGetChannels()
            this.channelList = data.data.channels
        },
        // 拿所有的channels，去除掉用户自己的channels，就是其他频道的内容
        async getAllChannels() {
            const { data } = await reqGetAllChannels()
            this.allChannelsList = data.data.channels
        },
        // 添加频道
        addNewChannel(channel: Channel) {
            this.channelList.push(channel)
        },
        // 删除频道
        deleteChannel(index: number) {
            this.channelList.splice(index, 1)
        }
    },
    getters: {
        // 登录或未登录展示的tab
        channelRenderList(): Channels {
            const user = useUserStore()
            return user.tokenInfo.token
                ? this.channelList
                : this.allChannelsList;
        },
        // 可添加的tab
        elseChannels(): Channels {
            return this.allChannelsList.filter(item => {
                return this.channelList.every(channel => channel.id !== item.id)
            })
        }
    }
})
export default useChannelStore